package edu.utn.tacs.grupouno.ui.server;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import edu.utn.tacs.grupouno.negocio.Analisis;
import edu.utn.tacs.grupouno.negocio.Turno;
import edu.utn.tacs.grupouno.negocio.TurnoSemanal;
import edu.utn.tacs.grupouno.rest.api.AnalisisService;
import edu.utn.tacs.grupouno.rest.api.TurnoService;
import edu.utn.tacs.grupouno.ui.client.TurnoUIService;
import edu.utn.tacs.grupouno.ui.client.dto.AnalisisDTO;
import edu.utn.tacs.grupouno.ui.client.dto.TurnoDTO;
import edu.utn.tacs.grupouno.ui.client.dto.TurnoSemanalDTO;
import edu.utn.tacs.grupouno.ui.server.factories.AnalisisDTOFactory;
import edu.utn.tacs.grupouno.ui.server.factories.TurnoDTOFactory;

public class TurnoUIServiceImpl extends RemoteServiceServlet implements TurnoUIService {

	private static final long serialVersionUID = 7111912700961373252L;
	private static Logger logger = Logger.getLogger(TurnoUIService.class.getCanonicalName());
	private ClassPathXmlApplicationContext context;

	@Override
	public List<TurnoDTO> getTurnos(String date) {
		try {
			logger.log(Level.FINE, "getTurnos(" + date + ")");
			List<TurnoDTO> retval = new ArrayList<TurnoDTO>();
			if(context == null) {
				context = new ClassPathXmlApplicationContext("beans-ui.xml");
			}
			AnalisisService analisisProxy = context.getBean("analisisService", AnalisisService.class);
			TurnoService turnoProxy = context.getBean("turnoService", TurnoService.class);
			List<Turno> turnos = turnoProxy.getTurnos(date);
			TurnoDTOFactory factory = new TurnoDTOFactory(analisisProxy);
			
			for (Turno turno : turnos) {
				logger.log(Level.FINE, "getTurnos: " + turno);
				retval.add(factory.create(turno));
			}
			return retval;
		} catch(Throwable e) {
			logger.log(Level.SEVERE, "No se pudieron obtener turnos");
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	@Override
	public void addTurnoSemanal(TurnoSemanalDTO dto) {
		try {
			logger.log(Level.FINE, "addTurnoSemanal");
			TurnoService turnoProxy = context.getBean("turnoService", TurnoService.class);
			TurnoDTOFactory factory = new TurnoDTOFactory();
			TurnoSemanal tsem = factory.create(dto);
			logger.log(Level.FINE, "turnoSemanal" + tsem);
			turnoProxy.addTurnoSemanal(tsem);
		} catch(Throwable e) {
			logger.log(Level.SEVERE, "No se pudo agregar un turno semanal");
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	@Override
	public void asignarAnalisis(TurnoDTO turno, AnalisisDTO dto) {
		try {
			logger.log(Level.FINE, "asignarAnalisis: id: " + dto.getId() + ", turno: " + turno);
			TurnoService turnoProxy = context.getBean("turnoService", TurnoService.class);
			String date = getDateString(turno.getFecha());
			AnalisisDTOFactory factory = new AnalisisDTOFactory();
			Analisis analisis = factory.create(dto);
			turnoProxy.asignarAnalisis(date, turno.getHorario(), analisis);
		} catch(Throwable t) {
			logger.log(Level.SEVERE, "No se pudo asignar analisis " + dto.getId());
			t.printStackTrace();
			throw new RuntimeException(t);
		}
	}
	
	private String getDateString(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeZone(TimeZone.getTimeZone("GMT"));
		cal.setTime(date);
		return cal.get(Calendar.DAY_OF_MONTH) + "-" + (cal.get(Calendar.MONTH)+1) + "-" +cal.get(Calendar.YEAR);
	}
}
